package com.llsydn.contentProvider;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;

/**
 * @author lilinshen
 * @title 操作person表的provider类
 * @description 请填写相关描述
 * @date 2018/9/30 15:37
 */
public class PersonProvider extends ContentProvider {
    private DBHelper dbHelper;

    //用来存放所有合法的uri容器
    private static UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);

    //content://com.llsydn.contentProvider.PersonProvider/person      不根据id操作
    //content://com.llsydn.contentProvider.PersonProvider/person/id   根据id操作
    static {
        uriMatcher.addURI("com.llsydn.contentProvider.PersonProvider", "/person", 1);
        uriMatcher.addURI("com.llsydn.contentProvider.PersonProvider", "/person/#", 2); //匹配任意数字
    }

    @Override
    public boolean onCreate() {
        Log.e("TAG", "onCreate()");
        dbHelper = new DBHelper(getContext());
        return false;
    }

    /**
     * 查询
     *
     * @param uri
     * @param projection
     * @param selection
     * @param selectionArgs
     * @param sortOrder
     * @return
     */
    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
        Log.e("TAG", "query()");
        SQLiteDatabase database = dbHelper.getReadableDatabase();
        //1.匹配uri,返回对应的code。
        int code = uriMatcher.match(uri);
        //如果合法，进行查询
        if (code == 1) {
            //不根据id查询
            Cursor cursor = database.query("person", projection, selection, selectionArgs, null, null, sortOrder);
            return cursor;
        } else if (code == 2) {
            //根据id查询
            Long id = ContentUris.parseId(uri);
            Cursor cursor = database.query("person", projection, "_id=?", new String[]{String.valueOf(id)}, null, null, null, sortOrder);
            return cursor;
        } else {
            //如果不合法，抛出异常
            throw new RuntimeException("查询的uri不合法");
        }
    }

    /**
     * 插入
     *
     * @param uri
     * @param values
     * @return
     */
    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
        Log.e("TAG", "insert()");
        SQLiteDatabase database = dbHelper.getReadableDatabase();
        //1.匹配uri,返回对应的code。
        int code = uriMatcher.match(uri);
        //如果合法，进行插入
        if (code == 1) {
            Long id = database.insert("person", null, values);
            //将id添加到uri中
            uri = ContentUris.withAppendedId(uri, id);
            database.close();
            return uri;
        } else {
            //如果不合法，抛出异常
            database.close();
            throw new RuntimeException("插入的uri不合法");
        }
    }

    /**
     * 删除
     *
     * @param uri
     * @param selection
     * @param selectionArgs
     * @return
     */
    @Override
    public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
        Log.e("TAG", "delete()");
        SQLiteDatabase database = dbHelper.getReadableDatabase();
        //1.匹配uri,返回对应的code。
        int code = uriMatcher.match(uri);
        int deleteCount = 0;
        //如果合法，进行删除
        if (code == 1) {
            //不根据id删除
            deleteCount = database.delete("person", selection, selectionArgs);
        } else if (code == 2) {
            //根据id删除
            Long id = ContentUris.parseId(uri);
            deleteCount = database.delete("person", "_id=?", new String[]{String.valueOf(id)});
        } else {
            //如果不合法，抛出异常
            database.close();
            throw new RuntimeException("查询的uri不合法");
        }
        database.close();
        return deleteCount;
    }

    /**
     * 更新
     *
     * @param uri
     * @param values
     * @param selection
     * @param selectionArgs
     * @return
     */
    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
        Log.e("TAG", "delete()");
        SQLiteDatabase database = dbHelper.getReadableDatabase();
        //1.匹配uri,返回对应的code。
        int code = uriMatcher.match(uri);
        int updateCount = 0;
        //如果合法，进行更新
        if (code == 1) {
            //不根据id更新
            updateCount = database.update("person", values, selection, selectionArgs);
        } else if (code == 2) {
            //根据id更新
            Long id = ContentUris.parseId(uri);
            updateCount = database.update("person", values, "_id=?", new String[]{String.valueOf(id)});
        } else {
            //如果不合法，抛出异常
            database.close();
            throw new RuntimeException("查询的uri不合法");
        }
        database.close();
        return updateCount;
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        Log.e("TAG", "getType()");
        return null;
    }
}
